package com.rui.cn.consumer.common;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
*@description：自定义异常处理
*@author：[zhangruilin]
*@className：MyErrorAttributes
*@time：2018/5/17-15:49
**/
@ControllerAdvice
public class MyErrorAttributes{
    //1、浏览器客户端返回的都是json
//    @ResponseBody
//    @ExceptionHandler(Exception.class)
//    public Map<String,Object> handleException(Exception e){
//        Map<String,Object> map = new HashMap<>();
//        map.put("code","user.notexist");
//        map.put("message",e.getMessage());
//        return map;
//    }
    /**
     *描述：异常处理
     *创建人：[zhangruilin]
     *方法名：handleException
     *创建时间：2018/5/17-15:44
     *参数：[e, request]
     *返回值：java.lang.String
     **/
    @ExceptionHandler(Exception.class)
    public String handleException(Exception e, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        request.setAttribute("javax.servlet.error.status_code",500);
        map.put("code","user.notexist");
        map.put("message","用户出错啦");
        request.setAttribute("ext",map);
        //转发到/error
        return "forward:/error";
    }
}